﻿using System;
using System.IO;
using System.Text;
using wojilu.Data;
using wojilu.IO;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Interface;
using YBBYunQi;

namespace wojilu.Web.Controller
{
    public class RenderHelper : IMvcFilter
    {

        private static readonly ILog logger = LogManager.GetLogger(typeof(RenderHelper));

        public virtual void Process(MvcEventPublisher publisher)
        {
            publisher.Begin_Render += new EventHandler<MvcEventArgs>(publisher_Begin_Render);
        }

        void publisher_Begin_Render(object sender, MvcEventArgs e)
        {
            String output = e.ctx.utils.getCurrentOutputString();
            var path = e.ctx.url.Path;
            if (path.ToLower().IndexOf("/big5/") > -1)
            {
                output = StringHelper.Trans(output, false);
            }

            // 加上页面执行时间
            output = output.Replace("</body>", getTimeJs() + "</body>");
            e.ctx.utils.setCurrentOutputString(output);
        }

        private static String getTimeJs()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script>var eleTime = document.getElementById('elapseTime');if( eleTime ) {");
            sb.AppendFormat("eleTime.innerHTML='{0}';", (WebStopwatch.Stop().ElapsedMilliseconds / 1000.0).ToString("0.0000"));
            sb.Append("};");
            sb.Append("var eleSql = document.getElementById('sqlQueries');if( eleSql ) {");
            sb.AppendFormat("eleSql.innerHTML='{0}';", DbContext.getSqlCount());
            sb.Append("}</script>");
            return sb.ToString();
        }
    }

}